package com.gzwanchuang.xhgj.ui.page.mine;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.gzwanchuang.xhgj.R;
import com.gzwanchuang.xhgj.bridge.request.MineRequestViewModel;
import com.gzwanchuang.xhgj.bridge.state.mine.MessageViewModel;
import com.gzwanchuang.xhgj.databinding.FragmentMessageBinding;
import com.gzwanchuang.xhgj.proto.MemberOuterClass;
import com.gzwanchuang.xhgj.ui.base.BaseFragment;

/**
 * 文件名 : HomeRootFragment
 * 创建者 : MangoWang
 * 创建日期 : 2019/12/24 16:20
 * 微信 : MangoWaWang
 * 邮箱 : 763482205@qq.com
 * 描述 : TODO
 */
public class MessageFragment extends BaseFragment {


    private FragmentMessageBinding mBinding;
    private MessageViewModel mViewModel;
    private MineRequestViewModel mRequestViewModel;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mViewModel = ViewModelProviders.of(this).get(MessageViewModel.class);
        mRequestViewModel = ViewModelProviders.of(this).get(MineRequestViewModel.class);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_message, container, false);
        mBinding = FragmentMessageBinding.bind(view);
        mBinding.setClick(new ClickProxy());
        mViewModel.layoutManager.set(new LinearLayoutManager(mActivity));
        mBinding.setVm(mViewModel);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mRequestViewModel.getErrorString().observe(this, s -> {
            hideProgress();
            showShortToast(s);
        });
        mRequestViewModel.getResultString().observe(this, s -> {
            hideProgress();
            showShortToast(s);
        });
        mRequestViewModel.getSystem_logs().observe(this, systemLogLists -> {
            hideProgress();
            mViewModel.adapter.get().addData(systemLogLists);
            int size = systemLogLists.size();
            if (size < 10) {
                mViewModel.adapter.get().getLoadMoreModule().loadMoreEnd();
            } else {
                mViewModel.adapter.get().getLoadMoreModule().loadMoreComplete();
            }
        });
        mRequestViewModel.system_log(mViewModel.page);

    }

    @Override
    public void loadInitData() {
        super.loadInitData();
    }

    public class ClickProxy {
        public void back() {
            nav().navigateUp();
        }

        public void itemOnClick(int position) {
            MemberOuterClass.SysteLog.SystemLogList bean = mViewModel.data.get(position);
        }

        public void loadMoreListener() {
            mViewModel.page = mViewModel.adapter.get().getData().size() / 10 + 1;
            mRequestViewModel.system_log(mViewModel.page);
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) { }
}
